package handler

import (
	"context"
	"github.com/google/martian/log"
	"ser-server/basic/config"
	"ser-server/internal/model"
	__ "ser-server/proto"
)

type DockerServer struct {
	__.UnimplementedDockerServer
}

func (d *DockerServer) LoginCreate(_ context.Context, req *__.LoginCreateReq) (*__.LoginCreateResp, error) {
	var user model.User
	err := config.DB.Where("phone = ?", req.Phone).Find(&user).Error
	if err != nil {
		return nil, err
	}
	newUser := model.User{
		Phone:    req.Phone,
		Username: req.Username,
		Password: req.Password,
	}
	config.DB.Create(&newUser)
	if req.Phone != newUser.Phone {
		log.Errorf("手机号输入错误,请重新输入", nil)
	}
	if newUser.Password != req.Password {
		log.Errorf("密码错误,请重新输入", nil)
	}
	return &__.LoginCreateResp{
		Code:    200,
		Message: "登录成功",
		Id:      int32(newUser.Id),
	}, nil
}
func (d *DockerServer) List(_ context.Context, req *__.DockerListReq) (*__.DockerListResp, error) {
	var hosp []*model.Hospital
	offset := (req.Page - 1) * req.PageSize
	config.DB.Offset(int(offset)).Order("created_at DESC").Where(int(req.PageSize)).Find(&hosp)

	var ListHosp []*__.DockerList
	for _, hospital := range hosp {
		ListHosp = append(ListHosp, &__.DockerList{
			HospitalName:        hospital.HospitalName,
			HospitalLabel:       hospital.HospitalLabel,
			FeaturedDepartments: hospital.FeaturedDepartments,
			Address:             hospital.Address,
			AppointmentNotice:   hospital.AppointmentNotice,
		})
	}
	return &__.DockerListResp{
		List: ListHosp,
	}, nil
}

func (d *DockerServer) ListDocker(_ context.Context, req *__.DockerListReq) (*__.DockerListResp, error) {
	var hosp []*model.Hospital
	offset := (req.Page - 1) * req.PageSize
	config.DB.Offset(int(offset)).Where(int(req.PageSize)).Find(&hosp)

	var ListHosp []*__.DockerList
	for _, hospital := range hosp {
		ListHosp = append(ListHosp, &__.DockerList{
			HospitalName:        hospital.HospitalName,
			HospitalLabel:       hospital.HospitalLabel,
			FeaturedDepartments: hospital.FeaturedDepartments,
			Address:             hospital.Address,
			AppointmentNotice:   hospital.AppointmentNotice,
		})
	}
	return &__.DockerListResp{
		List: ListHosp,
	}, nil
}
